<template>
  <div style="display: flex">
    <div class="right">
      <div class="header">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="频道名称">
            <el-input
              v-model="formInline.name"
              placeholder="请输入想要查询的频道名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="帐号状态">
            <el-select v-model="formInline.status" placeholder="帐号状态">
              <el-option label="开启状态" value="true"></el-option>
              <el-option label="禁止状态" value="false"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button type="primary" @click="addChannel">新建</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="middle">
        <el-table :data="tableData" stripe style="width: 1000px">
          <el-table-column prop="_id" label="序号" width="180">
          </el-table-column>
          <el-table-column prop="name" label="频道名称" width="180">
          </el-table-column>
          <el-table-column prop="content" label="描述" width="180">
          </el-table-column>
          <el-table-column prop="status" label="状态" width="180">
            <template slot-scope="thing">
              {{ thing.row.status ? "开启状态" : "禁止状态" }}
            </template>
          </el-table-column>
          <el-table-column prop="address" label="创建时间"
            >{{ new Date() }}
          </el-table-column>
          <el-table-column prop="do" label="操作">
            <template slot-scope="scope">
              <el-link type="primary" @click="update(scope.row)">编辑</el-link>
              <el-link type="success" @click="begin(scope.row)">启用</el-link>
              <el-link type="danger" @click="ban(scope.row)">禁用</el-link>
              <el-link type="danger" @click="del(scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[2, 3, 4, 5]"
            :page-size="100"
            layout="total, sizes, prev, pager, next, jumper"
            :total="count"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <el-dialog title="收货地址" :visible.sync="channelStatus">
      <el-form :model="form">
        <el-form-item label="频道名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="频道描述" :label-width="formLabelWidth">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启动" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status">
            <el-radio :label="true">启动</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="channelStatus = false">取 消</el-button>
        <el-button type="primary" @click="Channel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: {
        name: "",
        status: "",
      },
      channelStatus: false,
      formLabelWidth: "120px",
      currentPage: 1,
      pageSize: 2,
      count: 0,
      form: {
        name: "",
        content: "",
        status: "",
      },
      tableData: [],
    };
  },
  methods: {
    onSubmit() {
      this.getData();
    },
    addChannel() {
      this.channelStatus = true;
    },
    async Channel() {
      let {
        data: { code },
      } = await this.axios.post("/addChannel", this.form);
      if (code == 200) {
        this.form = {};
        this.channelStatus = false;
        this.getData();
      }
    },
    async getData() {
      let {
        data: { types, code, count },
      } = await this.axios.get(
        `/getTypes?pageSize=${this.pageSize}&currentPage=${this.currentPage}&name=${this.formInline.name}&status=${this.formInline.status}`
      );
      if (code == 200) {
        this.tableData = types;
        this.count = count;
      }
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
    async begin(data) {
      console.log(data);
      let{data:{code}} = await this.axios.get("/begin",{params:{id:data._id}})
      if(code==200){
        this.getData();
      }
    },
    async del(data) {
      let{data:{code}} = await this.axios.get("/del",{params:{id:data._id}})
      if(code==200){
        this.getData();
      }
    },
    async ban(data) {
      let{data:{code}} = await this.axios.get("/ban",{params:{id:data._id}})
      if(code==200){
        this.getData();
      }
    },
    async update(data) {
      let{data:{code}} = await this.axios.post("/update",data)
      if(code==200){
        this.getData();
      }
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style>
* {
  margin: 0;
  padding: 0;
}
.header {
  width: 1000px;
  height: 80px;
  /* border: 1px solid #000; */
}
.left {
  width: 250px;
  height: 700px;
}
.demo-form-inline {
  margin-top: 20px;
  margin-left: 20px;
}
</style>